package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.fragment.BillDataInFragment;
import com.tianxin.workbao.workBao.fragment.BillDataOutFragment;

/**
 * Created by hacker on 2017/7/25.
 */

public class BillDataActivity extends BaseActivity implements View.OnClickListener {

    private FloatingActionButton mListFab;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private int inOROutType;
    private int currentItem = 0;
    private FragmentStareAdapter mAdapter;
    private TextView pageTitleTV;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.in_out_data_activity);
        super.initStatusBar(false);
        Bundle bundle = getIntent().getExtras();
        this.inOROutType = bundle.getInt("type");
        this.currentItem = bundle.getInt("position");
        initView();
        initData();
        initEvent();
        bindView();


    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        mToolbar.setNavigationIcon(R.drawable.ic_action_back_24dp);
        pageTitleTV = findViewById(R.id.page_title);
        mListFab = findViewById(R.id.top_fab);
        mViewPager = findViewById(R.id.viewPager);
        mTabLayout = findViewById(R.id.tabLayout);

        mViewPager.setCurrentItem(currentItem);
        mViewPager.setOffscreenPageLimit(3);

        mTabLayout = findViewById(R.id.tabLayout);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.write));


    }

    @Override
    public void initData() {
        mTabTitles.add(0, "今天");
        mTabTitles.add(1, "本周");
        mTabTitles.add(2, "本月");
        mTabTitles.add(3, "本季");
        mTabTitles.add(4, "本年");

        if (this.inOROutType == Constant.TYPE_IN) {
            mFragments.add(BillDataInFragment.newInstance(Constant.TODAY_DATA,1));
            mFragments.add(BillDataInFragment.newInstance(Constant.WEEK_DATA,1));
            mFragments.add(BillDataInFragment.newInstance(Constant.MONTH_DATA,1));
            mFragments.add(BillDataInFragment.newInstance(Constant.SEASON_DATA,1));
            mFragments.add(BillDataInFragment.newInstance(Constant.YEAR_DATA,1));
        } else {
            mFragments.add(BillDataOutFragment.newInstance(Constant.TODAY_DATA,1));
            mFragments.add(BillDataOutFragment.newInstance(Constant.WEEK_DATA,1));
            mFragments.add(BillDataOutFragment.newInstance(Constant.MONTH_DATA,1));
            mFragments.add(BillDataOutFragment.newInstance(Constant.SEASON_DATA,1));
            mFragments.add(BillDataOutFragment.newInstance(Constant.YEAR_DATA,1));
        }
        mAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, mTabTitles);
    }

    @Override
    public void initEvent() {
        mToolbar.setNavigationOnClickListener(view -> BillDataActivity.this.finish());
        mListFab.setOnClickListener(this);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                searchTag = tab.getPosition();
                searchHint = tab.getText().toString();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void bindView() {
        mViewPager.setAdapter(mAdapter);
        if (inOROutType == Constant.TYPE_IN) {
            pageTitleTV.setText(getResources().getString(R.string.input_data));
        } else {
            pageTitleTV.setText(getResources().getString(R.string.output_data));
        }
        mListFab.setImageResource(R.drawable.ic_sync_);
    }

    //View.OnClickListener
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_fab:
                mFragments.get(searchTag).initData(false);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mFragments = null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                showSearchView(this, getSearchHandlerListener());
                break;
            case R.id.action_Share:
            case R.id.action_add:
                showToastMessage(this, getResources().getString(R.string.developing), 0);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
